package com.ja.openchat.ui.me;

import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.redpacket.RedPacketInfo;
import com.zy.openchat.core.model.remittance.RemittanceDetailInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.core.model.wallet.WalletExtData;
import com.zy.openchat.core.model.wallet.WalletOrderInfo;
import com.zy.openchat.util.TimeConvertUtils;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class TradeRecordAdapter extends BaseQuickAdapter<WalletOrderInfo, BaseViewHolder> implements LoadMoreModule {

    public TradeRecordAdapter() {
        super(R.layout.item_transaction_record);
    }

    private Map<Long,Object> extraDatas = new HashMap<>();

    private Map<Long,String> chatDatas = new HashMap<>();

    private String getChatInfo(final long chatId){
        if(chatDatas.containsKey(chatId)){
            return chatDatas.get(chatId);
        }
        UserInfo userInfo = UserInfo.getUserInfo(chatId);
        if(userInfo!=null){
            chatDatas.put(chatId,userInfo.displayName());
            return chatDatas.get(chatId);
        }
        TelegramManager.getTelegramManager().requestChatInfoByChatId(getContext(), ChatInfo.toLocalChatId(chatId), new TgResultBlock<ChatInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, ChatInfo obj) {
                if(obj!=null){
                    chatDatas.put(chatId,obj.title());
                    int length = getData().size();
                    for(int i = 0;i<length;i++){
                        RedPacketInfo redPacketInfo = getExtraRedPacketData(getItem(i).related);
                        if(redPacketInfo!=null&&redPacketInfo.chatId == chatId){
                            notifyItemChanged(i);
                        }
                    }
                }else{
                    TelegramManager.getTelegramManager().getUserSimpleInfo(chatId);
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });
        return "";
    }

    private Object getExtraData(final long extraId,int type){
        if(extraDatas.containsKey(extraId)){
            return extraDatas.get(extraId);
        }
        if(type==5||type==6){
            TelegramManager.getTelegramManager().queryRp(getContext(),extraId, new TgResultBlock<RedPacketInfo>() {
                @Override
                public void onResult(Map<String, Object> request, JsonObject response, RedPacketInfo obj) {
                    if(obj!=null){
                        putExtraData(extraId,obj);

                    }

                }

                @Override
                public void onTimeout(Map<String, Object> request) {

                }
            });
        }else if(type == 12||type == 13){
            TelegramManager.getTelegramManager().queryTransferMoney(getContext(), extraId, new TgResultBlock<RemittanceDetailInfo>() {
                @Override
                public void onResult(Map<String, Object> request, JsonObject response, RemittanceDetailInfo obj) {
                    if(obj!=null){
                        putExtraData(extraId,obj);
                    }
                }

                @Override
                public void onTimeout(Map<String, Object> request) {

                }
            });
        }
        return null;
    }

    private void putExtraData(final long extraId,Object object){
        extraDatas.put(extraId,object);
        int length = getData().size();
        for(int i = 0;i<length;i++){
            if(getItem(i).related == extraId){
                notifyItemChanged(i);
            }
        }
    }

    private RemittanceDetailInfo getExtraRemittanceData(long extraId){
        Object object = getExtraData(extraId,12);
        if(object!=null&&object instanceof RemittanceDetailInfo){
            return (RemittanceDetailInfo) object;
        }
        return null;
    }

    private RedPacketInfo getExtraRedPacketData(long extraId){
        Object object = getExtraData(extraId,5);
        if(object!=null&&object instanceof RedPacketInfo){
            return (RedPacketInfo) object;
        }
        return null;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, WalletOrderInfo walletOrderInfo) {
        String showContent = walletOrderInfo.getTypeContent();

        helper.setText(R.id.trade_date, TimeConvertUtils.formatDate9(walletOrderInfo.createAt*1000));
        helper.setText(R.id.trade_value,walletOrderInfo.amount>0?String.format("+%s",String.valueOf(walletOrderInfo.amount)):String.valueOf(walletOrderInfo.amount));
        helper.setTextColor(R.id.trade_status, ContextCompat.getColor(getContext(),walletOrderInfo.amount>=0?R.color.CG1:R.color.CG2));
        helper.setText(R.id.trade_status,walletOrderInfo.remarks);
        if(walletOrderInfo.extData!=null&&walletOrderInfo.extData.remittanceInfo!=null){
            RemittanceDetailInfo detailInfo = walletOrderInfo.extData.remittanceInfo;
            extraDatas.put(walletOrderInfo.related,detailInfo);
            if(walletOrderInfo.type == 13){
                UserInfo userInfo = UserInfo.getUserInfo(detailInfo.payerUID);
                if(userInfo!=null){
                    showContent = showContent+"-"+getContext().getString(R.string.from_text)+userInfo.displayName();
                }else{
                    TelegramManager.getTelegramManager().getUserSimpleInfo(detailInfo.payerUID);
                }
            }else{
                UserInfo userInfo = UserInfo.getUserInfo(detailInfo.payeeUID);
                if(userInfo!=null){
                    showContent = showContent+"-"+getContext().getString(R.string.transfer_to_text)+userInfo.displayName();
                }else{
                    TelegramManager.getTelegramManager().getUserSimpleInfo(detailInfo.payeeUID);
                }
            }
            helper.setText(R.id.trade_title,showContent);
        }else if(walletOrderInfo.type==5||walletOrderInfo.type==6||walletOrderInfo.type==7){
            RedPacketInfo redPacketInfo = getExtraRedPacketData(walletOrderInfo.related);
            if(redPacketInfo!=null){
                loadRedInfo(helper,walletOrderInfo,redPacketInfo);
            }else{
                helper.setText(R.id.trade_title,showContent);
            }

        }else if(walletOrderInfo.type == 12||walletOrderInfo.type == 13||walletOrderInfo.type == 14||walletOrderInfo.type == 15){
            RemittanceDetailInfo detailInfo = getExtraRemittanceData(walletOrderInfo.related);
            if(detailInfo!=null){
                walletOrderInfo.extData = new WalletExtData(detailInfo);
                convert(helper,walletOrderInfo);
            }else{
                helper.setText(R.id.trade_title,showContent);
            }

        }else{
            helper.setText(R.id.trade_title,showContent);
        }

    }

    private void loadRedInfo(BaseViewHolder helper,WalletOrderInfo walletOrderInfo,RedPacketInfo redPacketInfo){
        String showContent = walletOrderInfo.getTypeContent();
        if(redPacketInfo!=null){
            if(walletOrderInfo.type == 6){
                UserInfo userInfo = UserInfo.getUserInfo(redPacketInfo.from);
                if(userInfo!=null){
                    showContent = showContent+ "-"+getContext().getString(R.string.from_text)+userInfo.displayName();
                }else{
                    TelegramManager.getTelegramManager().getUserSimpleInfo(redPacketInfo.from);
                }
            }else{
                String chatName = getChatInfo(redPacketInfo.chatId);
                if(TextUtils.isEmpty(chatName)){
                    ChatInfo chatInfo = TelegramManager.getTelegramManager().getTgData().chatInfo(ChatInfo.toLocalChatId(redPacketInfo.chatId));
                    if(chatInfo!=null){
                        chatName = chatInfo.title;
                    }
                }

                showContent = showContent + "-"+getContext().getString(R.string.transfer_to_text)+chatName;
            }
        }
        helper.setText(R.id.trade_title,showContent);

    }


    @Override
    public @NotNull BaseLoadMoreModule addLoadMoreModule(@NotNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
        return new BaseLoadMoreModule(baseQuickAdapter);
    }
}
